//package test1;
//
//import com.fasterxml.jackson.annotation.JsonFormat;
//import net.sf.json.JSONObject;
//import org.apache.http.HttpHost;
//import org.apache.http.auth.AuthScope;
//import org.apache.http.auth.UsernamePasswordCredentials;
//import org.apache.http.client.CredentialsProvider;
//import org.apache.http.impl.client.BasicCredentialsProvider;
//import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//import org.apache.poi.ss.usermodel.Cell;
//import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.ss.usermodel.Sheet;
//import org.apache.poi.ss.usermodel.Workbook;
//
//import java.io.*;
//import java.util.HashMap;
//
///**
// * 疫情es导出，按照身份号，导出excel
// * <p>
// * 100.122.0.82
// * 100.122.0.85
// * 100.122.0.94
// * elasticsearch端口:19210 19211
// * 账号:elastic
// * 密码:Hljszwxxzyzxes137?!
// * Version: 6.5.4
// */
//public class es_to_excel {
//
//    private static String PATH = "./";//自己输出的路径
//
//    /**
//     *          {
//     *             "scantime": "2022-01-05 11:43:29",
//     *             "idcardnum": "230104198610154219",
//     *             "@timestamp": "2022-08-24T07:46:59.185Z",
//     *             "address": "哈尔滨融创乐园（世茂大道99号-1）",
//     *             "phone": "13613649513",
//     *             "name": "张文亮",
//     *             "@version": "1",
//     *             "code_name": null,
//     *             "idcardtype": 1,
//     *             "timestamp": "2022-08-24T15:46:59.185Z"
//     *             }
//     *
//     *             https://blog.csdn.net/weixin_42039228/article/details/123794466
//     */
//    public static void to_excel(SearchHits searchHits,String idcardnum) {
//        try {
//            //时间
//            long begin = System.currentTimeMillis();
//
//            //创建一个工作簿
//            Workbook workbook = new HSSFWorkbook();
//            //创建表
//            Sheet sheet = workbook.createSheet();
//            //创建行
//            Row row = sheet.createRow(0);
//            //创建列
//            Cell cell = row.createCell(0);
//            cell.setCellValue("姓名");
//
//            cell = row.createCell(1);
//            cell.setCellValue("身份证号");
//
//            cell = row.createCell(2);
//            cell.setCellValue("扫码时间");
//
//            cell = row.createCell(3);
//            cell.setCellValue("扫码地址");
//
////            cell = row.createCell(4);
////            cell.setCellValue("手机号");
//
//
//
//
////            for (SearchHit sh : searchHits) {
//            for (int rowNumber = 1; rowNumber <= searchHits.getTotalHits(); rowNumber++) {
//                String source = searchHits.getAt(rowNumber-1).getSourceAsString();
//                JSONObject jsonObject=JSONObject.fromObject(source);
//                //创建行
//                row = sheet.createRow(rowNumber);
//
//                //创建列
//                cell = row.createCell(0);
//                cell.setCellValue(jsonObject.getString("name"));
//
//                cell = row.createCell(1);
//                cell.setCellValue(jsonObject.getString("idcardnum"));
//
//                cell = row.createCell(2);
//                cell.setCellValue(jsonObject.getString("scantime"));
//
//                cell = row.createCell(3);
//                cell.setCellValue(jsonObject.getString("address"));
//
////                cell = row.createCell(4);
////                cell.setCellValue(jsonObject.getString("phone"));
//
//            }
//
//
//            System.out.println("结束！");
//            FileOutputStream fileOutputStream = new FileOutputStream(PATH + idcardnum+".xls");
//            workbook.write(fileOutputStream);
//            fileOutputStream.close();
//            long end = System.currentTimeMillis();
//            System.out.println("时间为：" + (double) (end - begin) / 1000);//2.262s
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//
//    public static void main(String[] args) throws IOException {
//
//        String queryJson = null;
//
//        RestClientBuilder builder = RestClient.builder(new HttpHost("100.122.0.85", 19210, "http"));
//        //开始设置用户名和密码
//        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "Hljszwxxzyzxes137?!"));
//        builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
//
//        RestHighLevelClient client = new RestHighLevelClient(builder);
//
//
//        SearchTemplateRequest templateRequest = new SearchTemplateRequest();
//        templateRequest.setRequest(new SearchRequest("yq_zdrysmxx"));
//        templateRequest.setScriptType(ScriptType.INLINE);
//
//
//        String fileName = "./idcardnum.txt";
//        File file = new File(fileName);
//        FileReader fr = new FileReader(file);
//        BufferedReader br = new BufferedReader(fr);
//        String line;
//        SearchResponse searchResponse = null;
//        SearchHits searchHits=null;
//        HashMap hashMap=new HashMap<>();
//        while((line = br.readLine()) != null){
//            //System.out.println(line);
//
//            queryJson="{\n" +
//                    "  \"query\": {\n" +
//                    "    \"bool\": {\n" +
//                    "      \"must\": [\n" +
//                    "        {\n" +
//                    "          \"match_phrase\": {\n" +
//                    "            \"idcardnum.keyword\": \""+line+"\"\n" +
//                    "          }\n" +
//                    "        }\n" +
//                    "      ]\n" +
//                    "    }\n" +
//                    "  },\n" +
//                    "  \"from\": 0,\n" +
//                    "  \"size\": 10000,\n" +
//                    "  \"sort\": [\n" +
//                    "    {\n" +
//                    "      \"scantime.keyword\": {\n" +
//                    "        \"order\": \"desc\"\n" +
//                    "      }\n" +
//                    "    }\n" +
//                    "  ]\n" +
//                    "}";
//
//            templateRequest.setScript(queryJson);
//            templateRequest.setScriptParams(hashMap);
//
//            searchResponse=client.searchTemplate(templateRequest, RequestOptions.DEFAULT).getResponse();
//
//            searchHits = searchResponse.getHits();
//            to_excel(searchHits,line);
//        }
//
//
//
//
//
//
//        client.close();
////        for (SearchHit sh : searchHits) {
////            String source = sh.getSourceAsString();
////            System.out.println(source);
////        }
//
//
//    }
//}
